import java.util.Random;

public class StringHelper {
	/**
	 * 1.返回len个长度随机数字
	 * 
	 * @param len
	 *            数字串长度
	 * @return String类型数字串
	 */
	public static String randomNumberString(int len) {
		if (len < 1) {
			return null;
		}
		Random random = new Random();
		int num = (int) Math.pow(10, len - 1);
		return (random.nextInt(9 * num) + num) + "";
	}

	/**
	 * 2.返回len个长度随机字母
	 * 
	 * @param len
	 *            字母串长度
	 * @return 返回len个长度随机字母String类型字符串
	 */
	public static String randomCharString(int len) {
		if (len < 1) {
			return null;
		}
		Random random = new Random();
		int temp, flag;
		char ch;
		StringBuffer string = new StringBuffer();
		for (int i = 0; i < len; i++) {
			flag = random.nextInt(2);
			temp = random.nextInt(26);
			if (flag == 1) {
				ch = (char) ('A' + temp);
				string.append(ch);
			} else {
				ch = (char) ('a' + temp);
				string.append(ch);
			}
		}
		return string.toString();
	}

	/**
	 * 3.返回len个长度随机字母和数字
	 * 
	 * @param len
	 *            字母和数字组合字符串长度
	 * @return 返回len个长度随机字母和数字String类型字符串
	 */
	public static String randomCharAndNumberString(int len) {
		if (len < 1) {
			return null;
		}
		Random random = new Random();
		int temp, flag;
		char ch;
		StringBuffer string = new StringBuffer();
		for (int i = 0; i < len; i++) {
			flag = random.nextInt(3);
			temp = random.nextInt(26);
			switch (flag) {
			case 2:
				ch = (char) ('A' + temp);
				string.append(ch);
				break;
			case 1:
				ch = (char) ('a' + temp);
				string.append(ch);
				break;
			case 0:
				temp = random.nextInt(10);
				ch = (char) ('0' + temp);
				string.append(ch);
				break;
			default:
				break;
			}
		}
		return string.toString();
	}

	/**
	 * 4. 传入123，返回中文数字壹贰叁
	 * 
	 * @param number
	 *            阿拉伯数字
	 * @return 中文数字串
	 */
	public static String convertToChinese(int number) {
		char[] chars = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒',
				'捌', '玖' };
		StringBuffer stringBuffer = new StringBuffer();
		String temp;
		int index;
		if (number < 0) {
			stringBuffer.append("负");
			temp = (number * -1) + "";
		} else if (number == 0) {
			return chars[0] + "";
		} else {
			temp = number + "";
		}
		for (int i = 0; i < temp.length(); i++) {
			index = Integer.parseInt(temp.charAt(i) + "");
			stringBuffer.append(chars[index]);
		}
		return stringBuffer.toString();
	}

	/**
	 * 5.在str里查找字符串string，whichone是查匹配的第几个？存在返回索引，否则返回-1
	 * 
	 * @param str
	 *            目标字符串
	 * @param search
	 *            要在str中查询的字符串
	 * @param whichOne
	 *            自左至右第几个匹配项,小于等于1返回第一个,其余值对应个数匹配
	 * @return
	 */
	public static int indexOf(String str, String search, int whichOne) {
		int index = str.indexOf(search);
		int indexS; // 查找中临时索引值
		String temp = str;// 中间变量字符串
		if (index > -1 && whichOne > 1) {
			temp = temp.substring(index + search.length());
			for (int i = 1; i < whichOne; i++) {
				indexS = temp.indexOf(search);
				if (indexS > -1) {
					index += (search.length() + indexS);
					temp = temp.substring(indexS + search.length());
				} else {
					index = -1;
					break;
				}
			}
		}
		return index;
	}

	/**
	 * 6.在str里查找字符串string，whichone是查匹配的第几个？存在返回索引，否则返回-1
	 * 
	 * @param str
	 *            目标字符串
	 * @param search
	 *            要在str中查询的字符串
	 * @param whichOne
	 *            自右至左第几个匹配项,小于等于1返回第一个,其余值对应个数匹配
	 * @return
	 */
	public static int lastIndexOf(String str, String search, int whichOne) {
		int index = str.lastIndexOf(search);
		String temp = str;// 中间变量字符串
		if (index > -1 && whichOne > 1) {
			temp = temp.substring(0, index);
			for (int i = 1; i < whichOne; i++) {
				index = temp.lastIndexOf(search);
				if (index > -1) {
					temp = temp.substring(0, index);
				} else {
					index = -1;
					break;
				}
			}
		}
		return index;
	}

	/**
	 * 7.传入文件名，得到文件扩展名
	 * 
	 * @param fileName
	 *            完整文件名
	 * @return 文件扩展名，null表示传入字符串无法解析出扩展名
	 */
	public static String getFileExtName(String fileName) {
		int index = fileName.lastIndexOf(".");
		if (index == -1 || index == fileName.length() - 1) {
			return null;
		} else {
			return fileName.substring(index + 1);
		}
	}

	/**
	 * 8. 在str里统计字符个数，注意CharType是枚举，
	 * 
	 * @param str
	 *            String类型字符串
	 * @param type
	 *            字符类型，值为CHAR：字母,NUMBER：数字,CharAndNumber：字母和数字，NotCharAndNumber：
	 *            非字母和数字
	 * @return 指定类型字符在这个字符串中的个数
	 */
	public static int length(String str, CharType type) {
		int count = 0;
		switch (type) {
		case CHAR:
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'
						|| str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
					count++;
				}
			}
			break;
		case NUMBER:
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
					count++;
				}
			}
			break;
		case CharAndNumber:
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'
						|| str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'
						|| str.charAt(i) >= '0' && str.charAt(i) <= '9') {
					count++;
				}
			}
			break;
		case NotCharAndNumber:
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'
						|| str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'
						|| str.charAt(i) >= '0' && str.charAt(i) <= '9') {
					count++;
				}
			}
			count = str.length() - count;
			break;

		default:
			break;
		}
		return count;
	}

	/**
	 * 9.判断str是不是字母构成
	 * 
	 * @param str
	 *            String类型字符串
	 * @return True表示是纯数字字符串，False表示不是
	 */
	public static boolean isNumber(String str) {
		return str.length() == length(str, CharType.NUMBER);
	}

	/**
	 * 10.判断str是不是字母构成
	 * 
	 * @param str
	 *            String类型字符串
	 * @return True表示是纯字母字符串，False表示不是
	 */
	public static boolean isLetter(String str) {
		return str.length() == length(str, CharType.CHAR);
	}

	/**
	 * 11.判断 mail是否是一个合法的电子邮件
	 * 
	 * @param mail
	 * @return
	 */

	public static Boolean isEmail(String mail) {
		int dotCount = 0;
		if (mail.startsWith("@") || mail.startsWith(".") || mail.endsWith("@")
				|| mail.endsWith(".")) {
			return false;
		}
		if (mail.lastIndexOf('@') > mail.indexOf('@')) {
			return false;
		}
		for (int i = 1; i < mail.length() - 1; i++) {
			if (mail.charAt(i) == '.') {
				dotCount++;
			}
		}
		if (dotCount == 0 || dotCount > 2) {
			return false;
		}
		if (mail.contains("@.")) {
			return false;
		}
		if (mail.indexOf("@") > mail.indexOf(".")) {
			return false;
		}
		if (mail.lastIndexOf('.') == mail.indexOf('.')
				|| mail.lastIndexOf('.') > mail.indexOf('.') + 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 12.判断这个字符串是不是邮政编码
	 * 
	 * @param code
	 *            字符串
	 * @return True是邮编格式，False不是
	 */
	public static boolean isZipCode(String code) {
		return isNumber(code) && code.length() == 6;
	}

	/**
	 * 13.判断card是不是身份证号码
	 * 
	 * @param card
	 *            字符串
	 * @return True是身份证号码格式，False不是
	 */
	public static boolean isCardId(String card) {
		int len = card.length();
		String temp = card.substring(0, len - 1);
		boolean flag = false;
		if (len != 18) {
			return false;
		}
		if (card.charAt(len - 1) == 'x' || card.charAt(len - 1) == 'X'
				|| card.charAt(len - 1) >= '0' && card.charAt(len - 1) <= '9') {
			flag = true;
		}
		return flag && isNumber(temp);
	}
}
